import {
    Entity,
    Column,
    ManyToOne,
    OneToMany,
    JoinColumn,
} from 'typeorm';
import { Base } from '@/entities.base/base.entity';
import { Customer } from '../customer.entity';
import { OrderService } from '../order-service.entity';
import { OrderPointsUsage } from '../order-points-usage.entity';
import { OrderStoredValueUsage } from '../order-stored-value-usage.entity';
import { OrderGroupBuyUsage } from '../order-group-buy-usage.entity';

@Entity('basis_order')
export class Order extends Base {
    /** 订单编号 - 唯一标识订单的编号 */
    @Column({
        type: 'varchar',
        length: 50,
        unique: true,
        comment: '订单编号',
    })
    orderNumber: string;

    /** 订单状态 - pending(待支付)/paid(已支付)/completed(已完成)/cancelled(已取消) */
    @Column({
        type: 'varchar',
        length: 20,
        default: 'pending',
        comment: '订单状态(pending/paid/completed/cancelled)',
    })
    orderStatus: string;

    /** 订单总金额 - 包含所有费用的总金额 */
    @Column({
        type: 'decimal',
        precision: 10,
        scale: 2,
        default: 0,
        comment: '订单总金额',
    })
    totalAmount: number;

    /** 折扣金额 - 应用的折扣总金额 */
    @Column({
        type: 'decimal',
        precision: 10,
        scale: 2,
        default: 0,
        comment: '折扣金额',
    })
    discountAmount: number;

    /** 折扣原因 - 申请折扣的原因说明 */
    @Column({
        type: 'varchar',
        length: 200,
        nullable: true,
        comment: '折扣原因',
    })
    discountReason?: string;

    /** 积分抵扣金额 - 使用积分抵扣的金额 */
    @Column({
        type: 'decimal',
        precision: 10,
        scale: 2,
        default: 0,
        comment: '积分抵扣金额',
    })
    pointsDeductionAmount: number;

    /** 储值卡抵扣金额 - 使用储值卡抵扣的金额 */
    @Column({
        type: 'decimal',
        precision: 10,
        scale: 2,
        default: 0,
        comment: '储值卡抵扣金额',
    })
    storedValueDeductionAmount: number;

    /** 现金支付金额 - 实际现金支付的金额 */
    @Column({
        type: 'decimal',
        precision: 10,
        scale: 2,
        default: 0,
        comment: '现金支付金额',
    })
    cashPaymentAmount: number;

    /** 团购支付总金额 - 使用团购方式支付的总金额 */
    @Column({
        type: 'decimal',
        precision: 10,
        scale: 2,
        default: 0,
        comment: '团购支付总金额',
    })
    groupBuyPaymentAmount: number;

    /** 赠送积分 - 订单完成后赠送给客户的积分 */
    @Column({
        type: 'decimal',
        precision: 10,
        scale: 2,
        default: 0,
        comment: '赠送积分',
    })
    giftPoints: number;

    /** 订单时间 - 订单创建的时间 */
    @Column({
        type: 'datetime',
        default: () => 'CURRENT_TIMESTAMP',
        comment: '订单时间',
    })
    orderTime: Date;

    /** 完成时间 - 订单完成的时间 */
    @Column({
        type: 'datetime',
        nullable: true,
        comment: '完成时间',
    })
    completedTime?: Date;

    /** 备注 - 订单相关的备注信息 */
    @Column({
        type: 'text',
        nullable: true,
        comment: '备注',
    })
    remark?: string;

    /** 客户ID - 关联的客户标识 */
    @Column({
        type: 'varchar',
        length: 36,
        comment: '客户ID',
    })
    customerId: string;

    // 关联关系
    /** 客户信息 - 多对一关联客户表 */
    @ManyToOne(() => Customer, customer => customer.orders, {
        onDelete: 'CASCADE',
    })
    @JoinColumn({ name: 'customerId' })
    customer: Customer;

    /** 订单服务列表 - 一对多关联订单服务表 */
    @OneToMany(() => OrderService, orderService => orderService.order, {
        cascade: true,
    })
    services: OrderService[];

    /** 积分使用记录 - 一对多关联积分使用记录表 */
    @OneToMany(() => OrderPointsUsage, pointsUsage => pointsUsage.order, {
        cascade: true,
    })
    pointsUsages: OrderPointsUsage[];

    /** 储值卡使用记录 - 一对多关联储值卡使用记录表 */
    @OneToMany(() => OrderStoredValueUsage, storedValueUsage => storedValueUsage.order, {
        cascade: true,
    })
    storedValueUsages: OrderStoredValueUsage[];

    /** 团购使用记录 - 一对多关联团购使用记录表 */
    @OneToMany(() => OrderGroupBuyUsage, groupBuyUsage => groupBuyUsage.order, {
        cascade: true,
    })
    groupBuyUsages: OrderGroupBuyUsage[];
} 